package com.inke.box;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.inke.box.login.LoginActivity;
import com.inke.box.net.minasocket.InkeMinaClientService;
import com.inke.box.net.minasocket.InkeMinaServer;
import com.inke.box.net.minasocket.MinaClientConnCallBack;
import com.inke.box.net.minasocket.MinaServerCallBack;
import com.inke.box.net.minasocket.SessionSendUtils;

import org.apache.mina.core.session.IoSession;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends Activity {

    @BindView(R.id.ip)TextView ip;
    @BindView(R.id.send_btn)Button send_btn;
    @BindView(R.id.send_msg)EditText send_msg;

    private IoSession session;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        findViewById(R.id.tologin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
            }
        });

    }



    @OnClick({R.id.start_btn,R.id.start_client_btn,R.id.send_btn,R.id.send_br_btn})
    public void startServerOnclick(View view){
        switch (view.getId()){
            case R.id.start_btn:
                //启动Mina服务器
                startMinaServer();
                break;
            case R.id.start_client_btn://启动客户端扫描服务器
                InkeMinaClientService.getInstance().setMinaClientConnCallBack(new MinaClientConnCallBack() {
                    @Override
                    public void onConnectSucceend(IoSession session) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showShort("连接成功");
                            }
                        });

                        MainActivity.this.session = session;
                    }

                    @Override
                    public void onConnectError(final String msg) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showShort(msg);
                            }
                        });
                    }
                }).connLoopServerIp();
                break;
            case R.id.send_btn:
                String msg = send_msg.getText().toString().trim();
                if (!StringUtils.isEmpty(msg)){
                    if (session!=null)
                        session.write(msg+"\r");
                }else {
                    ToastUtils.showShort("请不要发送空信息");
                }
                break;
            case R.id.send_br_btn:
                boolean b = SessionSendUtils.getIns().sendMsg("服务器");
                if (b){
                    ToastUtils.showShort("发送成功");
                }else {
                    ToastUtils.showShort("发送失败");
                }
                break;
        }
    }

    private void startMinaServer() {
        InkeMinaServer.getInstance().setMinaServerCallBack(new MinaServerCallBack() {

            @Override
            public void onStarFinsh(final String msg) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ip.setText(msg);
                        ToastUtils.showShort("启动成功");
                    }
                });
            }
        }).serverStart();
    }

}
